package presentation.web;

import jaxcent.HtmlButton ;
import jaxcent.HtmlInputText ;
import jaxcent.HtmlPara ;
import jaxcent.Jaxception ;
import util.Log ;
import util.NotFoundException ;
import util.Subsystem ;
import domain.user.Privilege ;
import domain.user.UnpermittedActionException ;

/**
 * The Class AdminPage. Responsible for updating regular user to moderator
 *
 * @author Hadas
 */
public class AdminPage extends MainPage
{
	/** The username text field. */
	private HtmlInputText username;

	/** The errors paragraph contains error messages to the user. */
	private HtmlPara errorsPara;

	/**
	 * Instantiates a new Admin page.
	 */
	public AdminPage()
	{
		super();
		try
		{
			if (!this.verifyUserAllowedPage(Privilege.GIVE_PERMISSION))
			{
				return ;
			}

			this.username = new HtmlInputText(this,"newModeratorName");
			this.setErrors();
			this.makeButton();
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null);
		}
	}

	/**
	 * Moderator button handler.
	 * Handles pressing the make moderator button including all corresponding domain services
	 */
	protected void moderatorButtonHandler()
	{
		try
		{
			this.errorsPara.setInnerHTML("" );
			String newModeratorUsername =  this.username.getValue();
			//checks if user enter username to become moderator
			if(newModeratorUsername.equals(""))
			{
				this.errorsPara.setInnerHTML("Missing username!" );
			}
			else
			{
				this.getForumLogic().addModeratorPrivileges(this.getUser().getId(),newModeratorUsername);
				this.errorsPara.setStyle("color", "blue");
				this.errorsPara.setInnerHTML("user " + newModeratorUsername + " was made forum moderator");
				this.pageWait();
				Log.getLogger(Subsystem.PRESENTATION_MODEL).finest("user " + newModeratorUsername + " was made forum moderator");
				this.navigateToPage(MainPage.HOME_PAGE) ;
			}
		}
		//user not allowed
		catch(UnpermittedActionException e)
		{
			try
			{
				this.errorsPara.setInnerHTML("dont have permission");
				this.username.setValue("");
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("unpermitted action exception") ;
			}
			catch (Jaxception e1)
			{
				this.navigateToErrorPage(e1, this.errorsPara);
			}
		}
		//Username is not registered in forum system
		catch(NotFoundException e)
		{
			try
			{
				this.username.setValue("");
				this.errorsPara.setInnerHTML("username not found - enter correct username");
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("username  not in system. maybe typed incorrect user") ;
			}
			catch (Jaxception e1)
			{
				this.navigateToErrorPage(e1, this.errorsPara);
			}
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null) ;
		}
	}//End moderatorButtonHandler

	/**
	 * Create make moderator button.
	 * Create correspond override on Click method
	 */
	private void makeButton(){
		try
		{
			new HtmlButton(this,"makeModerator"){
		        @Override
				protected void onClick() {
		        	AdminPage.this.moderatorButtonHandler();
		        }
		    };
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null) ;
		}
	}

	/**
	 * Make the current page wait for 2 second for user to see the message on screen.
	 */
	private void pageWait()
	{
		try
		{
			Thread.sleep(2000);
		}
		catch (InterruptedException e)
		{
			//interrupt the page before 2000 mili is ok
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null) ;
		}
	}

	/**
	 * Sets the errors paragraph with initialize empty text.
	 */
	private void setErrors()
	{
		try
		{
			this.errorsPara = new HtmlPara(this,"adminErrors");
			this.errorsPara.setInnerHTML("");
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, this.errorsPara) ;
		}
	}
}

